<?php

declare(strict_types=1);

namespace App\Dao;

use App\Model\RechargeAmount as Model;
use App\Model\RechargeAmountChannel;

class RechargeAmountDao
{
    use DaoTrait;

    public function __construct()
    {
        $this->driver = __CLASS__;
    }

    //获取所有内容
    public function getAll()
    {
        $data = Model::with('type')
            ->orderBy('id', 'desc')
            ->get()->each(function ($value) {
                $value->append(['status_mapping'])->forceFill([
                    'type_name' => $value->type->name,
                ])->makeHidden('type');
            });

        if (!empty($data)) {
            $ctime = time();
            foreach ($data as $k => $v) {
                $data[$k]['channel_list'] =
                    RechargeAmountChannel::query()
                    ->leftJoin('channel', 'recharge_amount_channel.channel_id', '=', 'channel.id')
                    ->where(['recharge_amount_channel.recharge_amount_id' => $v['id'], 'channel.status' => 1, 'channel.is_del' => 1])
                    ->where('channel.start_time', '<', $ctime)->whereIn('channel.type', [$v['type_id'], 3])
                    ->orderBy('channel.sort', 'desc')
                    ->get(['recharge_amount_channel.id', 'channel.name', 'recharge_amount_channel.status'])->toArray();
            }
        }
        return $data;
    }


    //根据条件获取所有内容
    public function getByWhere($map = [], $fields = [], $scope = [],$orderBy='id')
    {   
        $model = Model::with('type');
        if (!empty($fields)) {
            $model->select($fields);
        }
        $data=$model->when(!empty($map),function($query) use($map){
            $query->where($map);
        })->orderBy($orderBy, 'desc')->get()->each(function ($value) {
            $value->append(['status_mapping'])->forceFill([
                'type_name' => $value->type->name,
            ])->makeHidden('type');
        });
        return $data;
    }

    /**
     * 判断充值金额是否存在,存在返回正常状态的 -id
     * @Author czm
     * @DateTime 2020-04-20
     * @param [type] $ori_amount 初始金额
     * @param [type] $type_id  1快充 2慢充 
     * @return mixed
     */
    public function isLegalAmount($ori_amount, $type_id)
    {
        $id = Model::where('type_id', $type_id)->where('ori_amount', $ori_amount)->where('status', 1)->value('id');
        if (!empty($id)) {
            return $id;
        } else {
            return false;
        }
    }


    /**
     * 判断充值折扣金额是否合法,存在返回正常状态的 -id
     * @Author czm
     * @DateTime 2020-04-20
     * @param [type] $ori_amount 初始金额
     * @param [type] $amount 售价金额
     * @param [type] $type_id  1快充 2慢充  
     * @return mixed
     */
    public function isLegalDisCountAmount($ori_amount,$amount, $type_id)
    {
        $id = Model::where('type_id', $type_id)
            ->where('amount', $amount)
            ->where('ori_amount', $ori_amount)->where('status', 1)->value('id');
        if (!empty($id)) {
            return $id;
        } else {
            return false;
        }
    }

    /**
     * 查询
     * @param $where
     * @param string[] $columns
     * @return array
     */
    public function searchByWhere($where, $columns=['*']){
        return Model::query()->where($where)->orderBy('id','desc')->get($columns)->toArray();
    }
}
